package com.crm.system.service.impl.contactManager;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.beust.jcommander.internal.Lists;
import com.crm.common.model.DhPageInfo;
import com.crm.common.model.Enum.StatisticalPeriodEnum;
import com.crm.common.model.Enum.StatusCodeEnum;
import com.crm.common.util.CommonUtils;
import com.crm.common.util.DateUtil;
import com.crm.common.util.DateUtils;
import com.crm.model.dto.InsertContactCallable;
import com.crm.model.entity.cluePoolManager.ClueFollow;
import com.crm.model.entity.cluePoolManager.ClueVisitRecord;
import com.crm.model.entity.contactManager.Contact;
import com.crm.model.entity.contactManager.ContactInformation;
import com.crm.model.entity.contactManager.ContactInformationManyPeople;
import com.crm.model.entity.contactManager.ContactInformationManyPeopleName;
import com.crm.model.entity.contractManager.CustomerContract;
import com.crm.model.entity.customerManager.Customer;
import com.crm.model.entity.customerManager.CustomerContactPerson;
import com.crm.model.entity.customerManager.CustomerPool;
import com.crm.model.entity.sys.UserAccount;
import com.crm.model.entity.sys.permissionManagement.SysCompany;
import com.crm.model.qo.cluePoolManager.ClueFollowQO;
import com.crm.model.qo.contactManager.ContactInformationManyPeopleQO;
import com.crm.model.qo.contactManager.ContactQO;
import com.crm.model.vo.contactManager.*;
import com.crm.model.vo.customerManager.CustomerContactVO;
import com.crm.model.vo.customerManager.FzCustomerContactVO;
import com.crm.model.vo.systemManager.ContactImportVO;
import com.crm.service.cluePoolManager.ClueFollowDbService;
import com.crm.service.cluePoolManager.ClueVisitRecordDbService;
import com.crm.service.contactManager.ContactDbService;
import com.crm.service.contactManager.ContactInformationDbService;
import com.crm.service.contactManager.ContactInformationManyPeopleDbService;
import com.crm.service.contractManager.CustomerContractDbService;
import com.crm.service.customerManager.*;
import com.crm.service.sys.ImportFailureLogDbService;
import com.crm.service.sys.UserAccountDbService;
import com.crm.service.sys.permissionManagement.SysCompanyDbService;
import com.crm.system.service.contactManager.ContactInformationManyPeopleService;
import com.crm.system.service.contactManager.ContactInformationService;
import com.crm.system.service.contactManager.ContactService;
import com.crm.system.service.impl.cluePoolManager.ClueFollowServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.*;
import java.util.stream.Collectors;

/**
 * bj
 * 联系人管理信息
 */
@Service
@Slf4j
public class ContactInformationManyPeopleServiceImpl implements ContactInformationManyPeopleService {

    @Autowired
    private ContactInformationManyPeopleDbService contactInformationManyPeopleDbService;

    @Autowired
    private SysCompanyDbService companyDbService;

    /**
     * 联系联系方式一机多用
     *
     * @param pageIndex 页码
     * @param pageSize  分页大小
     * @param sortName  排序列名字
     * @param sortOrder 排序规则(asc/desc)
     * @param contactInformationManyPeopleQO 查询条件
     * @return
     */
    @Override
    public DhPageInfo<ContactInformationManyPeopleVO> contactInformationManyPeopleList(Integer pageIndex, Integer pageSize, ContactInformationManyPeopleQO contactInformationManyPeopleQO, String sortName, String sortOrder) {
        if (pageIndex != null && pageSize != null) {
            PageHelper.startPage(pageIndex, pageSize);
        }

        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        boolean rs = CommonUtils.isAdmin(userAccount.getUserId());

        //增加删除状态查询条件(0:正常;1:已删除)
        contactInformationManyPeopleQO.setDeleteStatus((short) StatusCodeEnum.NORMAL_CODE.getState());
        Integer dateUnit = 0;
        if (!StringUtils.isBlank(contactInformationManyPeopleQO.getDateType())) {
            if ("thisWeek".equals(contactInformationManyPeopleQO.getDateType())) {
                //本周
                dateUnit = 1;
            }
            if ("thisMonth".equals(contactInformationManyPeopleQO.getDateType())) {
                //本月
                dateUnit = 2;
            }
        }


        List<ContactInformationManyPeopleVO> list = new ArrayList<ContactInformationManyPeopleVO>();
        if ("0".equals(contactInformationManyPeopleQO.getBs())) {
            if (rs) {
                //超级管理员
                list = contactInformationManyPeopleDbService.contactInformationManyPeopleList(contactInformationManyPeopleQO,dateUnit,0);
            } else {
                //普通管理员
                contactInformationManyPeopleQO.setCompanyId(userAccount.getCompanyId());
                list = contactInformationManyPeopleDbService.contactInformationManyPeopleList(contactInformationManyPeopleQO, dateUnit,1);
            }
        } else {
            //普通用户
            contactInformationManyPeopleQO.setCompanyId(userAccount.getCompanyId());
            contactInformationManyPeopleQO.setAccountId(userAccount.getAccountId());
            list = contactInformationManyPeopleDbService.contactInformationManyPeopleList(contactInformationManyPeopleQO, dateUnit,2);
        }


        PageInfo<ContactInformationManyPeopleVO> pageInfo = new PageInfo<ContactInformationManyPeopleVO>(list);
        return new DhPageInfo<ContactInformationManyPeopleVO>(pageInfo, list);
    }

    /**
     * 新增联系方式人
     * @param contactInformationManyPeople
     * @return
     */
    @Override
    public String addContactInformationManyPeople(ContactInformationManyPeople contactInformationManyPeople) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        SysCompany sysCompany = companyDbService.selectSysCompanyById(userAccount.getCompanyId());

        ContactInformationManyPeople people = contactInformationManyPeopleDbService.selectManyPeopleByPhoneAndAccountId(contactInformationManyPeople.getContactInformation(),userAccount.getAccountId());
        if(people == null) {
            contactInformationManyPeople.setContactInformationManyPeopleId(CommonUtils.getGUID());
            contactInformationManyPeople.setCreatePeopleId(userAccount.getAccountId());
            contactInformationManyPeople.setCreateTm(new Date());
            contactInformationManyPeople.setUpdatePeopleId(userAccount.getAccountId());
            contactInformationManyPeople.setUpdateTm(new Date());
            contactInformationManyPeople.setDeleteStatus(Short.parseShort("0"));
            contactInformationManyPeople.setAbolishState(1);
            contactInformationManyPeople.setAccountId(userAccount.getAccountId());
            contactInformationManyPeople.setUserName(userAccount.getUserName());
            contactInformationManyPeople.setCompanyId(userAccount.getCompanyId());
            if(sysCompany != null) {
                contactInformationManyPeople.setCompanyName(sysCompany.getCompanyName());
            }
            contactInformationManyPeopleDbService.addContactInformationManyPeople(contactInformationManyPeople);
            return "SUCCESS";
        }else{
            return "联系方式已存在,请检查后重试!";
        }


    }

    /**
     * 联系人名称管理信息
     * @param contactInformationManyPeopleId 联系方式一机多用id
     * @param bs 0显示未废止的  1显示全部的
     * @return
     */
    @Override
    public DhPageInfo<ContactInformationManyPeopleName> contactInformationManyPeopleNameList(String contactInformationManyPeopleId,String bs,Integer pageIndex, Integer pageSize) {
        if (pageIndex != null && pageSize != null) {
            PageHelper.startPage(pageIndex, pageSize);
        }
        List<ContactInformationManyPeopleName> list = contactInformationManyPeopleDbService.contactInformationManyPeopleNameList(contactInformationManyPeopleId,bs);
        PageInfo<ContactInformationManyPeopleName> pageInfo = new PageInfo<ContactInformationManyPeopleName>(list);
        return new DhPageInfo<ContactInformationManyPeopleName>(pageInfo,list);
    }

    /**
     * 新增联系人名称
     * @param name
     * @return
     */
    @Override
    public String addContactInformationManyPeopleName(ContactInformationManyPeopleName name) {

        ContactInformationManyPeople con = contactInformationManyPeopleDbService.selectContactInformationManyPeople(name.getContactInformationManyPeopleId());
        if(name.getContactName().equals(con.getContactName())) {
            return "联系人名称不能跟之前的一样";
        }
        List<ContactInformationManyPeopleName> list = contactInformationManyPeopleDbService.contactInformationManyPeopleNameList(name.getContactInformationManyPeopleId(), "1");
        for(ContactInformationManyPeopleName peopleName : list) {
            if(name.getContactName().equals(peopleName.getContactName())) {
                return "联系人名称不能跟之前的一样";
            }
        }


        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        name.setContactInformationManyPeopleNameId(CommonUtils.getGUID());
        name.setCreatePeopleId(userAccount.getAccountId());
        name.setCreateTm(new Date());
        name.setUpdatePeopleId(userAccount.getAccountId());
        name.setUpdateTm(new Date());
        name.setDeleteStatus(Short.parseShort("0"));
        name.setAbolishState(1);
        Boolean bs = contactInformationManyPeopleDbService.addContactInformationManyPeopleName(name);
        return "success";
    }

    /**
     * 切换联系人名称
     * @param name
     * @return
     */
    @Override
    public Boolean switchName(ContactInformationManyPeopleName name) {
        String contactName = name.getContactName();
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        ContactInformationManyPeople contactInformationManyPeople = contactInformationManyPeopleDbService.selectContactInformationManyPeople(name.getContactInformationManyPeopleId());



        name.setContactName(contactInformationManyPeople.getContactName());
        name.setUpdatePeopleId(userAccount.getAccountId());
        name.setUpdateTm(new Date());
        Boolean rs = contactInformationManyPeopleDbService.updateContactInformationManyPeopleName(name);
        if(rs) {
            contactInformationManyPeople.setContactName(contactName);
            contactInformationManyPeople.setUpdatePeopleId(userAccount.getAccountId());
            contactInformationManyPeople.setUpdateTm(new Date());
            contactInformationManyPeopleDbService.updateContactInformationManyPeople(contactInformationManyPeople);
        }
        return rs;
    }

    /**
     * 废止联系人名称
     * @param name
     * @return
     */
    @Override
    public Boolean abolishName(ContactInformationManyPeopleName name) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        name.setUpdatePeopleId(userAccount.getAccountId());
        name.setUpdateTm(new Date());
        return contactInformationManyPeopleDbService.updateContactInformationManyPeopleName(name);
    }
}
